<template>
<div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-form :inline="true"  size="mini" :model="search">
          <el-form-item label="关键字 " >
            <el-input placeholder="根据编号搜索" v-model="search.content"></el-input>
          </el-form-item>
          <el-form-item class="button">
              <el-button @click="clear">清除</el-button>
              <el-button type="primary" @click="searchList">搜索</el-button>
          </el-form-item>
        </el-form>
        <el-alert
          :title="`数据一共${total}条`"
          type="info"
          show-icon>
        </el-alert>
        <div>
          <el-table
            :data="tableData"
            style="width: 100%">
            <el-table-column
              prop="id"
              label="编号"
              width="180">
            </el-table-column>
            <el-table-column
            label="题型"
              width="100"
            prop="questionType"
            :formatter="(tableData) => {
              switch (tableData.questionType) {
                case '1':
                  return '单选';
                case '2':
                  return '多选';
                case '3':
                  return '简答';
                default:
                  return '未知';
              }
            }"
          ></el-table-column>
          <el-table-column
            class="ItemNum"
            label="题目编号"
            width="240"
          >
            <template slot-scope="scope">
              <div v-for="(item, index) in scope.row.questionIDs" :key="index" >
                <a class="qusett"  :href="item.link" target="_blank" @click="Detail(item.id )">{{ item.number }}</a>
              </div>
            </template>
          </el-table-column>
            <el-table-column
              prop="addTime"
              label="录入时间"
              width="180">
            </el-table-column>
            <el-table-column
              prop="totalSeconds"
              label="答题时间(s)"
              width="110">
            </el-table-column>
            <el-table-column
              prop="accuracyRate"
              label="正确率(%)"
              width="180">
            </el-table-column>
            <el-table-column
              prop="userName"
              label="录入人"
              width="180">
            </el-table-column>
            <el-table-column
              prop="address"
              label="操作">
              <template slot-scope="scope">
                <el-button type="danger" icon="el-icon-delete" circle plain @click="del(scope.row.id)"></el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="page">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-size="20"
            :current-page.sync="page"
            :page-sizes="[20, 30, 50, 100]"
            layout="prev, pager, next,sizes, jumper"
            :total="total">
          </el-pagination>
        </div>
      </el-card>
    </div>
    <div>
          <el-dialog title="题目预览" :visible.sync="dialogVisible" width="900px">
            <el-row>
              <el-col :span="6"
                >【题型】：{{questType}}</el-col
              >
              <el-col :span="5">【编号】：{{item.id}}</el-col>
              <el-col :span="5">【难度】：{{different }}</el-col>
              <el-col :span="8" class="tagBox">
                      【标签】：<span class="a" v-html="item.tags"></span>
                </el-col>
              <el-col :span="6">【学科】：{{item.direction }}</el-col>
              <el-col :span="5">【目录】：{{item.directoryName }}</el-col>
              <el-col :span="5">【方向】：{{item.direction}}</el-col>
              <el-col :span="8"></el-col>
            </el-row>
            <el-divider></el-divider>
            <div>【题干】：<span style="color: #0000ff" v-html="item.question"></span></div>
            <div>
              <div v-if="questType === '单选'">
              选项：（以下选中的选项为正确答案）
                <div class="option">
                  <el-radio onClick="javascript:return false" v-for="itea in item.options" :key="itea.id" v-model="itea.isRight" :label="1">{{
                    itea.title
                  }}</el-radio>
                </div>
              </div>
              <div class="option" v-else-if="questType === '多选'">
              选项：（以下选中的选项为正确答案）
                <div v-for="itea in item.options" :key="itea.id">
                  <el-checkbox onClick="javascript:return false" v-model="itea.isRight" :true-label="1" :false-label="0">{{
                    itea.title
                  }}</el-checkbox>
                </div>
              </div>
            </div>
            <el-divider></el-divider>
            <div>【参考答案】：<el-button @click="open" type="danger">视频答案预览</el-button></div>
            <div>
              <video width="320" height="240" controls v-show="videoOpen">
                <source :src="item.videoURL" type="video/mp4" />
              </video>
            </div>
            <el-divider></el-divider>
            <div style="height: 30px; line-height: 30px; display: flex; align-items: center">
              【答案解析】：
              <div v-html="item.answer"></div>
            </div>
            <el-divider></el-divider>
            <div style="height: 30px; line-height: 30px; display: flex; align-items: center">
              【题目备注】：
              <div >{{item.remarks}}</div>
            </div>
            <span slot="footer" class="dialog-footer">
              <el-button type="primary" @click="isdialog">关闭</el-button>
            </span>
          </el-dialog>
        </div>
  </div>
</template>

<script>
import { randoms, detail, removeRandoms } from '@/api/hmmm/questions.js'

export default {
  data () {
    return {
      different: 1,
      questType: '',
      videoOpen: false,
      item: {},
      dialogVisible: false,
      search: {
        content: ''
      },
      total: '',
      page: 1,
      pagesize: 10,
      tableData: []
    }
  },
  methods: {

    open () {
      this.videoOpen = true
    },
    async Detail (value) {
      this.dialogVisible = true
      const { data: res } = await detail({ id: value })
      this.item = res
      if (this.item.questionType === '1') {
        this.questType = '单选'
      } else if (this.item.questionType === '2') {
        this.questType = '多选'
      } else {
        this.questType = '简答'
      }
      if (this.item.difficulty === '1') {
        this.different = '简单'
      } else if (this.item.questionType === '2') {
        this.different = '中等'
      } else {
        this.different = '困难'
      }
    },
    async clear () {
      this.search.content = ''
      const { data: res } = await randoms()
      this.tableData = res.items
      this.total = res.counts
    },
    async handleCurrentChange (value) {
      const { data: res } = await randoms({
        page: value
      })
      this.tableData = []
      this.tableData = res.items
      this.total = res.counts
    },
    async searchList () {
      this.page = 1
      console.log(this.tableData)
      console.log(this.search.content)
      if (this.search.content) {
        // const { data: res } = await randoms({
        //   keyword: this.search.content
        // })
        // this.tableData = []
        // this.tableData = res.items
        // this.total = res.counts
        this.tableData = this.tableData.filter(ele => ele.id === this.search.content)
      } else if (this.search.content === '') {
        const { data: res } = await randoms()
        this.tableData = res.items
        this.total = res.counts
      }
    },
    async handleSizeChange (value) {
      const { data: res } = await randoms({
        pagesize: value
      })
      this.tableData = []
      this.tableData = res.items
      this.total = res.counts
    },
    isdialog () {
      this.dialogVisible = false
      this.videoOpen = false
      console.log(this.dialogVisible)
    },
    async del (value) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        console.log(value)
        const { data: res } = await removeRandoms({
          id: value
        })
        this.tableData = this.tableData.filter(ele => ele.id !== value)
        const { data: ress } = await randoms({
          pagesize: value
        })
        this.total = ress.counts
        console.log(this.tableData)
        console.log(res)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  },
  async created () {
    try {
      const { data: res } = await randoms()
      this.tableData = res.items
      this.total = res.counts
    } catch (error) {
      console.log(error)
    }
  },
  watch: {
    dialogVisible (newValue) {
      this.videoOpen = false
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form{
  display: flex;
  justify-content: space-between;
}
.page{
  margin-left:500px;
}
::v-deep .el-table_1_column_3{
  color: rgb(44, 142, 203);
}
::v-deep .has-gutter  .el-table_1_column_3{
  color: rgb(151, 151, 151) ;
}

.quset{
  margin-left: 5px;
  color: blue;
}
.qusett{
  color: blue;
}
.el-pagination {
    white-space: nowrap;
    padding: 35px 5px;
    color: #303133;
    font-weight: 700;
}
::v-deep .el-icon-arrow-up{
  margin-left: 305px;
}
.el-row {
  .el-col {
    line-height: 40px;
  }
}
.el-divider {
  margin: 8px 0;
  background-color: #000;
}
.option {
  ::v-deep .el-radio {
    display: block;
    line-height: 30px;
  }
  ::v-deep .el-checkbox {
    display: block;
    line-height: 30px;
  }
}
.el-dialog__footer{
  text-align: right;
}

.tagBox{
  width: 278px;
position: relative;
  .a{
    position: absolute;
    left: 66px;
    top: 0px;
  }
}

p{
  margin:3px
}

::v-deep .el-input__suffix-inner {
    pointer-events: none;
}
</style>
